﻿using IJhbyService;
using JhbyCommon.CommonModel;
using JhbyHelper;
using JhbyModel.Models;
using JhbyWebApi.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JhbyWebApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class DictionaryController : ControllerBase
    {
        jhbyrbacContext _context = null;
        //注入
        IDictionarysService _dictionarysService;
        public DictionaryController(IDictionarysService dictionarysService)
        {
            _dictionarysService = dictionarysService;
        }
        /// <summary>
        /// 字典列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="ParentID"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("GetList")]
        public string GetList(int pageIndex, int pageSize, int ParentID)
        {
            Response response1 = new Response();
            //token验证 
            int verifytokenid = TokenHelper.VerifyToken(HttpContext.Request);
            response1.Code = verifytokenid;
            if (verifytokenid == 0)
            {
                response1.data = _dictionarysService.GetList(pageIndex, pageSize, ParentID);
            }
            else if (verifytokenid == 1)
            {
                response1.message = "访问用户不存在！";
            }
            else if (verifytokenid == 2)
            {
                response1.message = "Token验证失败！";
            }
            else
            {
                response1.message = "账号已经在其他地方登陆！";
            }
            return JsonConvert.SerializeObject(response1);
        }
        /// <summary>
        /// 字典添加/编辑
        /// </summary>
        /// <param name="dictionary"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Edit")]
        public string Edit(Dictionary dictionary)
        {
            return JsonConvert.SerializeObject(_dictionarysService.Edit(dictionary));
        }

        /// <summary>
        /// 获取父级ID
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("DicTionList")]
        public string DicTionList(int ParentId)
        {
            return JsonConvert.SerializeObject(_dictionarysService.DicTionList(ParentId));
        }
    }
}
